package com.itheima.ExceptionAll;

import com.itheima.commit.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class Exceptions {

    @ExceptionHandler({SQLIntegrityConstraintViolationException.class})
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
        if (ex.getMessage().contains("Duplicate entry")) {
            String[] s = ex.getMessage().split(" ");
            String msg = s[2]+"已存在";
            return R.error(msg);
        }
        return R.error("系统繁忙");
    }

    @ExceptionHandler(MyCustomException.class)
    public R<String> customException(MyCustomException myCustomException){
        return R.error(myCustomException.getMessage());
    }
}
